/* ***********************************************************************
 * 项目名称:油耗
 * 项目组:
 * 创建时间:2017.7.11
 * 创建人:马方印
 * 用途:出油或入油页面
 * 版本:1.0
 * 修改记录:    修改内容   修改原因   修改人   修改时间
 *               无        无       无       无
 *************************************************************************/

#import "YZInputOrOutputGasolineViewController.h"
#import "YZCustomRecordBtn.h"//自定义记录按钮
#import "YZOilTankCell.h"
#import "SetPwdViewController.h"
#import "YHTimeViewController.h"
#import "MFYAnimationManager.h"//页面跳转动画
#import "YZInputOrOutputGasolineRecordViewController.h"//记录页面
#import "YZInputOrOutputGasolineModel.h"
#import "ChooseDateView.h"

@interface YZInputOrOutputGasolineViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *listArray;//油罐

@property (nonatomic, strong) NSArray *listErrorArray;//记录

@property (nonatomic, strong) UIButton *backBtn;//返回按钮


@end

static NSString *identifier = @"cell";

@implementation YZInputOrOutputGasolineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
//    self.navigationItem.title = @"2017-07-13";
    
    
    [self.view addSubview:self.titleLabel];
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.backBtn];
}

#pragma mark - 入油 出油 数据请求
- (void)getInputCount_CopyWithType:(NSString *)type{
    //    3bc25ad91d674732b8802e28ed53a3f8 油站ID
    
    NSDictionary *params = @{
                             
                             @"type" : type,
                             @"starttime" : self.beginTime,
                             @"endtime" : self.endTime,
                             @"iolId" : kIolID
                             
                             };
    
    [MBProgressHUD postLoadViewWithText:HUD_PLEASEWAIT View:kWindow];
    
    [ClientRequest SOAPMthod:@"getInputCount_Copy" soapBody:params success:^(id responseObject) {
        
        [MBProgressHUD hideLoadViewWithView:kWindow];
        
        YZInputOrOutputGasolineModel *model = [YZInputOrOutputGasolineModel mj_objectWithKeyValues:responseObject];
        
        NSLog(@"油罐 - %ld",model.list.count);
        NSLog(@"记录 - %ld",model.listerro.count);
        
        self.listArray = model.list;
        self.listErrorArray = model.listerro;
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD postMessageWithText:@"请求失败" View:kWindow];
        
        NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
        NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"服务器的错误原因：%@",str);
    }];
}

#pragma mark - 入油记录 出油记录 加油记录 按钮
//进入记录页面
- (void)showRecordButtonClick:(UIButton *)sender{
    
    //设置转场动画
    MFYAnimationManager *manager = [MFYAnimationManager shareMFYAnimationManater];
    manager.animationTime = 0.25;
    manager.presentedViewFrame = CGRectMake(0, kScreenHeight*0.4-60, kScreenWidth, kScreenHeight*0.6);
    
    YZInputOrOutputGasolineRecordViewController *recordVC = [[YZInputOrOutputGasolineRecordViewController alloc]init];
    recordVC.modalPresentationStyle = UIModalPresentationCustom;
    recordVC.transitioningDelegate = manager;
    
    if (sender.tag == 100 || sender.tag == 101) {//入油 出油
        
        recordVC.type = sender.tag - 100;//0 入油页面 1 出油页面
        recordVC.listErrorArray = self.listErrorArray;
        [self presentViewController:recordVC animated:NO completion:nil];
    }
    else{//加油
        
        NSDictionary *params = @{
                                 
                                 @"starttime" : self.beginTime,
                                 @"endtime" : self.endTime,
                                 
                                 };
        
        [MBProgressHUD postLoadViewWithText:HUD_PLEASEWAIT View:kWindow];
        
        [ClientRequest SOAPMthod:@"outAddIolList" soapBody:params success:^(id responseObject) {
            
            [MBProgressHUD hideLoadViewWithView:kWindow];
            
            YZInputOrOutputGasolineModel *model = [YZInputOrOutputGasolineModel mj_objectWithKeyValues:responseObject];
            
            NSLog(@"车辆 - %ld",model.carList.count);
            
            recordVC.type = sender.tag - 100;//0 入油页面 1 出油页面 2 加油页面
            recordVC.listErrorArray = model.carList;
            [self presentViewController:recordVC animated:NO completion:nil];
            
        } failure:^(NSError *error) {
            
            [MBProgressHUD postMessageWithText:@"请求失败" View:kWindow];
            
            NSData *data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
            NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"服务器的错误原因：%@",str);
        }];
    }
}

//创建
- (void)setType:(NSString *)type{
    _type = type;
    //input 入油页面 output 出油页面
    
    if ([type isEqualToString:@"input"]) {
        
        self.titleLabel.text = @"入油油罐";
        
        //入油记录
        YZCustomRecordBtn *RYRecordBtn = [self creatRecordBtnWithTitle:@"入油记录" andTag:100];
        RYRecordBtn.frame = CGRectMake(0, kScreenHeight - 60, kScreenWidth, 60);
        [RYRecordBtn addTarget:self action:@selector(showRecordButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:RYRecordBtn];
        
        [self getInputCount_CopyWithType:@"0"];
    }
    else{
        
        self.titleLabel.text = @"出油油罐";
        
        CGFloat w = (kScreenWidth - 5) / 2;//宽度
        
        //出油记录
        YZCustomRecordBtn *CYRecordBtn = [self creatRecordBtnWithTitle:@"出油记录" andTag:101];
        CYRecordBtn.frame = CGRectMake(0, kScreenHeight - 60, w, 60);
        [CYRecordBtn addTarget:self action:@selector(showRecordButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:CYRecordBtn];
        //加油记录
        YZCustomRecordBtn *JYRecordBtn = [self creatRecordBtnWithTitle:@"加油记录" andTag:102];
        JYRecordBtn.frame = CGRectMake(w + 5, kScreenHeight - 60, w, 60);
        [JYRecordBtn addTarget:self action:@selector(showRecordButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:JYRecordBtn];
        
        [self getInputCount_CopyWithType:@"1"];
    }
}

//自定义记录按钮
- (YZCustomRecordBtn *)creatRecordBtnWithTitle:(NSString *)title andTag:(NSInteger)tag{
    
    YZCustomRecordBtn *btn = [[YZCustomRecordBtn alloc]init];
    [btn setTitle:title forState:UIControlStateNormal];
    btn.tag = tag;
    
    return btn;
}

#pragma mark - 返回按钮监听
- (void)backButtonClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - collectionView delegate and datasource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.listArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    YZOilTankCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    
    YZListModel *listModel = self.listArray[indexPath.row];
    cell.currentType = self.type;
    cell.listModel = listModel;
    
    return cell;
}

#pragma mark - 懒加载
- (UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(150, kScreenHeight - 60 - 64 - 54);
        layout.sectionInset = UIEdgeInsetsMake(0, 25, 0, 25);
//        layout.minimumInteritemSpacing = 25;
        layout.minimumLineSpacing = 25;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64 + 54, kScreenWidth, kScreenHeight - 60 - 64 - 54) collectionViewLayout:layout];
        _collectionView.showsHorizontalScrollIndicator = NO;
        //        _collectionView.backgroundColor = COLOR(234, 234, 232, 1);
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerClass:[YZOilTankCell class] forCellWithReuseIdentifier:identifier];
    }
    
    return _collectionView;
}

- (UILabel *)titleLabel{
    
    if (!_titleLabel) {
        
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.text = @"入油油罐";
        _titleLabel.font = [UIFont systemFontOfSize:18.0f];
        _titleLabel.textColor = COLOR(0, 153, 204, 1);
        _titleLabel.frame = CGRectMake(0, 64, kScreenWidth, 44);
        _titleLabel.backgroundColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _titleLabel;
}

- (UIButton *)backBtn{
    
    if (!_backBtn) {
        
        _backBtn = [[UIButton alloc]init];
        _backBtn.frame = CGRectMake(10, 64, 100, 44);
        _backBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -60, 0, 0);
        [_backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        [_backBtn addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _backBtn;
}



//时间选择
- (void)DateSelectedButtonClick:(UIButton *)sender{
    
    if (!self.dateView) {
        
        self.dateView = [[ChooseDateView alloc]initWithFrame:self.view.bounds];
        self.dateView.backgroundColor = [UIColor whiteColor];
        [self.dateView setHidden:NO];
        
        __weak typeof(self) weakSelf = self;
        
        self.dateView.selectDateCompletionHandle = ^(NSString *beginTime, NSString *endTime){
            
            NSLog(@"%@ - %@",beginTime, endTime);
            
            weakSelf.beginTime = beginTime;
            weakSelf.endTime = endTime;
            
            if ([weakSelf.type isEqualToString:@"input"]) {
             
                [weakSelf getInputCount_CopyWithType:@"0"];
            }
            else if ([weakSelf.type isEqualToString:@"output"]){
                
                [weakSelf getInputCount_CopyWithType:@"1"];
            }
        };
        
        [self.view addSubview:self.dateView];
    }
    else{
        
        [self.dateView setHidden:NO];
    }
}

@end
